package app.mvc.controller.editor
{
	import app.consts.CommandType;
	import app.interfaces.IEditorCommand;
	import app.mvc.AppFacade;
	import app.mvc.Notifications;
	import app.mvc.model.VO.ControlsBarVO;
	import app.mvc.model.VO.UIObjectVO;
	import app.mvc.model.proxys.EditorDataProxy;
	
	import globals.Config;
	
	import org.puremvc.as3.interfaces.ICommand;
	import org.puremvc.as3.interfaces.INotification;
	import org.puremvc.as3.patterns.command.SimpleCommand;
	
	import utils.UtilsGUID;
	import utils.UtilsXML;
	
	/**
	 * 新增一个控件
	 * @author xiling
	 */
	public class AddObjectCommand extends EditorBaseCommand implements ICommand, IEditorCommand
	{
		public function AddObjectCommand()
		{
			super();
		}		
		
		override public function redo():void
		{
			super.redo();
			execute(_notification);
		}
		
		override public function undo():void
		{
			super.undo();
			var obj:Object=  _notification.getBody();
			AppFacade.getInstance().sendNotification(Notifications.CMD_TO_REMOVE_OBJECT,[obj.uid],CommandType.UNDO);
		}
		
		override public function execute(notification:INotification):void
		{
			// TODO Auto Generated method stub
			super.execute(notification);
			var obj:Object=  notification.getBody();
			var label:String = obj.label;
			if(label)
			{
				var uiObjectVO:UIObjectVO= new UIObjectVO();
				uiObjectVO.uid = obj.uid;
				var xmlItem:XML = UtilsXML.getChildByParents(Config.configData,"item","label",label);
				var key:String="";
				var value:String="";
				for each(var attr:XML in xmlItem.attributes())
				{
					key = attr.name();
					value = attr;
					if(uiObjectVO.hasOwnProperty(key))
					{
						uiObjectVO[key] = value;
					}
				}
				uiObjectVO.x = notification.getBody().x;
				uiObjectVO.y = notification.getBody().y;
				
				EditorDataProxy.getInstance().addObject(uiObjectVO);
				AppFacade.getInstance().sendNotification(Notifications.TO_ADD_OBJECT,uiObjectVO);
			}
		}
		
	}
}